import {createRouter, createWebHistory} from "vue-router";
import {useUserStore} from "@/stores/user";

// @ts-ignore

const routers=[
    {
        path: '/',
        name: 'Home',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/Home.vue'),
        children:[
            {
                path:'allgz',
                name:'AllGz',
                meta: { public: false},
                // @ts-ignore
                component: () => import('@/components/AllGuanZhu.vue'),
            },
            {
                path:'notice',
                name:'Notice',
                meta: { public: false},
                // @ts-ignore
                component: () => import('@/components/Notice.vue'),
            },
            {
                path:'',
                name:'Post',
                // @ts-ignore
                component: () => import('@/components/Post.vue'),
            },
            {
                path:'poster',
                name:'poster',
                // @ts-ignore
                component: () => import('@/components/Poster.vue'),
            },
            {
                path:'hot',
                name:'hot',
                // @ts-ignore
                component: () => import('@/components/wzHotSearch.vue'),
            },
            {
                path:'hoet',
                name:'Hoet',
                meta: { public: true},
                // @ts-ignore
                component: () => import('@/pages/hoet.vue'),
            },
            {
                path:'new',
                name:'New',
                // @ts-ignore
                component: () => import('@/components/NewPost.vue'),
            },
            {
                path:'hotPost',
                name:'HotPost',
                // @ts-ignore
                component: () => import('@/components/HotPost.vue'),
            },

            {
                path:'search',
                name:'search',
                // @ts-ignore
                component: () => import('@/components/search.vue'),
            },

        ]
    },
    {
        path: '/yycsh',
        name: 'yycsh',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/LogSign.vue'),
        children:[
            {
                path:'',
                name:'Login',
                // @ts-ignore
                component: () => import('@/pages/Login.vue'),
                meta: { public: true },
            },
            {
                path:'sign',
                name:'Sign',
                // @ts-ignore
                component: () => import('@/pages/Register.vue'),
                meta: { public: true },
            },
            {
                path:'forget',
                name:'Forget',
                // @ts-ignore
                component: () => import('@/pages/ForgetRegister.vue'),
                meta: { public: true },
            },
        ]
    },
    {
        path: '/video',
        name: 'Video',
        meta: { public: true },
        // @ts-ignore
        component: () => import('@/pages/Video.vue'),
        children:[
            {
                path:'',
                name:'TuiJian',
                // @ts-ignore
                component: () => import('@/pages/video/TuiJian.vue'),
            },
            {
                path:'open',
                name:'Open',
                // @ts-ignore
                component: () => import('@/pages/video/OpenVideo.vue'),
            },
        ]
    }


]
const router = createRouter({
    history: createWebHistory(),
    routes: routers
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()

    // 如果不是公开路由且未登录
    if (!to.meta.public && !userStore.isLoggedIn) {
        next({ name: 'Login'})
    } else {
        next()
    }
})
export default router